<!--
描述：直播课开课
开发人：zhaohang
开发日期：2019年11月20日
-->
<template lang="pug">
  kalix-dialog.user-add(title='开课' bizKey="liveExamine" ref="kalixBizDialog" v-bind:formModel.sync="formModel" v-bind:targetURL="targetURL" :submitCustom="submitCustom")
    div.el-form(slot="dialogFormSlot")
      el-form-item(label="开课" prop="temp")
        el-select(v-model="formModel.temp"  placeholder="请选择")
          el-option(v-for="item in option" v-bind:key="item.value" v-bind:value="item.value" v-bind:label="item.label")
</template>

<script type="text/ecmascript-6">
  import {liveURL, classNoticeURL} from '../../config.toml'
  import FormModel from './model'
  import Message from '../../../../../common/message'
  import EventBus from '../../../../../components/corelib/common/eventbus'
  import {ON_REFRESH_DATA} from '../../../../../components/corelib/components/common/event.toml'

  export default {
    name: 'liveOpen',
    data() {
      return {
        temp: null,
        studentNotice: null,
        fileList: [],
        formModel: Object.assign({}, FormModel),
        option: [{
          value: 1,
          label: '开课'
        }, {
          value: 2,
          label: '取消开课'
        }
        ],
        labelWidth: '120px',
        targetURL: liveURL
      }
    },
    mounted() {
      this.temp = this.formModel.isOpen
      if (this.temp === 0) {
        this.temp = '待审核'
      }
      if (this.temp === 1) {
        this.temp = '通过'
      }
      if (this.temp === 2) {
        this.temp = '不通过'
      }
    },
    methods: {
      submitCustom(baseDialog) {
        if (baseDialog.formModel.appraise === '否') {
          this.formModel.appraise = 'false'
        } else {
          this.formModel.appraise = 'true'
        }
        if (baseDialog.formModel.temp === '开课') {
          this.formModel.temp = 1
        }
        if (baseDialog.formModel.temp === '取消开课') {
          this.formModel.temp = 2
        }
        if (baseDialog.formModel.isExamine === '通过') {
          this.formModel.isExamine = 1
        } else if (baseDialog.formModel.isExamine === '不通过') {
          this.formModel.isExamine = 2
        } else if (baseDialog.formModel.isExamine === '待审核') {
          this.formModel.isExamine = 0
        }
        this.formModel.isOpen = this.formModel.temp
        if (this.formModel.isExamine !== 0) {
          if (baseDialog.formModel.isOpen === 1) {
            this.formModel.notice = '您申请的《' + this.formModel.className + '》课已开课！'
            this.studentNotice = '您报名的《' + this.formModel.className + '》课已开课！'
            this.formModel.isLiveOpen = 't'
          } else {
            this.formModel.notice = '您申请的《' + this.formModel.className + '》课因人数不足已取消开课！'
            this.studentNotice = '您报名的《' + this.formModel.className + '》课因人数不足已取消开课！'
          }
          this.axios.request({
            method: 'GET',
            url: liveURL + '/' + this.formModel.id,
            data: {
              id: this.formModel.id
            }
          }).then(res => {
            this.axios.request({
              method: 'PUT',
              url: liveURL + '/' + this.formModel.id,
              data: {
                id: this.formModel.id,
                className: this.formModel.className,
                classIntroduce: this.formModel.classIntroduce,
                teacherIntroduce: this.formModel.teacherIntroduce,
                classCover: this.formModel.classCover,
                isOpen: this.formModel.isOpen,
                isExamine: this.formModel.isExamine,
                appraise: this.formModel.appraise,
                openTime: this.formModel.openTime,
                browseNum: this.formModel.browseNum,
                teacherId: this.formModel.teacherId,
                personNum: this.formModel.personNum,
                nowNum: res.data.nowNum,
                overTime: this.formModel.overTime,
                courseware: this.formModel.courseware,
                reason: this.formModel.reason,
                isLiveOpen: this.formModel.isLiveOpen
              }
            }).then(response => {
              this.axios.request({
                method: 'POST',
                url: classNoticeURL,
                data: {
                  notice: this.formModel.notice,
                  personName: this.formModel.teacher,
                  new: true,
                  personId: this.formModel.teacherId
                }
              }).then(response => {
                this.axios.request({
                  method: 'GET',
                  url: classNoticeURL + '/liveNotice',
                  params: {
                    classId: this.formModel.id,
                    studentNotice: this.studentNotice
                  }
                }).then(response => {
                  if (baseDialog.formModel.isOpen === 2) {
                    this.axios.request({
                      method: 'delete',
                      url: liveURL + '/' + this.formModel.id,
                      data: {
                        id: this.formModel.id
                      }
                    }).then(response => {
                    })
                  }
                  if (response.data.success) {
                    Message.success(response.data.msg)
                    EventBus.$emit('liveExamine' + '-' + 'KalixDialogClose')
                    // this.$refs.dialogForm.resetFields()
                    // this.submitComplete()
                  } else {
                    Message.error(response.data.msg)
                    EventBus.$emit('liveExamine' + '-' + 'KalixDialogClose')
                  }
                  // }
                  // 刷新列表
                  console.log(' ===== BaseDialog ON_REFRESH_DATA ===== ', ON_REFRESH_DATA)
                  EventBus.$emit(ON_REFRESH_DATA, 'liveOpen')
                  console.log('[kalix] dialog submit button clicked !')
                })
              })
            })
          })
        } else {
          Message.error('请先进行审核！')
          EventBus.$emit('liveExamine' + '-' + 'KalixDialogClose')
          console.log(' ===== BaseDialog ON_REFRESH_DATA ===== ', ON_REFRESH_DATA)
          EventBus.$emit(ON_REFRESH_DATA, 'liveOpen')
          console.log('[kalix] dialog submit button clicked !')
        }
      }
    }
  }
</script>

<style scoped lang="stylus" type="text/stylus">

</style>
